package main

import (
	"flag"
	"fmt"
	"os"
)

type Cmd struct {
	helpFlag  bool
	store     bool
	openapiFP string
	apiPath   string
	tag       string
	base      string
	release   bool
	show      bool
	flush     bool
	delete    bool
}

/*
1. swd base "filepath" | 选择基础openapi文件路径
2. swd release | 释放选择的基础openapi文件

3. swd	show | 展示所有api以及其tag标签
4. swd store "tag" "apiPath" | 这里对包含有apiPath这个路径的所有api以映射方式存储到json文件中，
先将所有api打印出来，然后 yes(y) or no(n) 进行选择是否真的写入到json
5. swd flush "jsonPath" | 扫描json将新增的tag填充到openapi文件中然后将json置空
*/
func parseCmd() *Cmd {
	cmd := &Cmd{}
	flag.Usage = printUsage
	flag.BoolVar(&cmd.helpFlag, "help", false, " print help msg")
	flag.BoolVar(&cmd.helpFlag, "h", false, " print help msg")
	flag.StringVar(&cmd.openapiFP, "filePath", "", "openapi FilePath")
	flag.StringVar(&cmd.apiPath, "api", "", "api parameter")
	flag.StringVar(&cmd.tag, "tag", "", "tag for new group")
	flag.StringVar(&cmd.base, "base", "", "Selected openapi file to be processed")
	flag.BoolVar(&cmd.release, "release", false, "Release the selected openapi file")
	flag.BoolVar(&cmd.show, "show", false, "show all apis' information")
	flag.BoolVar(&cmd.store, "store", false, "Loads the api you want to change to the specified tag")
	flag.BoolVar(&cmd.flush, "flush", false, "Refresh the data to the openapi file")
	flag.BoolVar(&cmd.delete, "delete", false, "Delete selected apis")
	flag.Parse()

	// 获取位置参数
	args := flag.Args()
	if len(args) >= 2 {
		cmd.tag = args[0]
		cmd.apiPath = args[1]
	}

	return cmd
}

func printUsage() {
	// format
	fmt.Println("______________________________________________")
	fmt.Printf(Yellow+"Usage: #%s [-options] filePath\n"+Reset, os.Args[0])
	fmt.Printf(Yellow + "选择基础openapi文件: [-filePath]\n" + Reset)
	fmt.Printf(Yellow + "释放选择的基础文件: [-release]\n" + Reset)
	fmt.Printf(Yellow + "展示所有api: [-show]\n" + Reset)
	fmt.Printf(Yellow + "加载需要更改的api到零时json文件: [-store] [-tagName] apiPath\n" + Reset)
	fmt.Printf(Yellow + "将更改加载到openapi文件中: [-flush] \n" + Reset)
	fmt.Printf(Yellow + "删除临时json里的api: [-del] \n" + Reset)
	fmt.Println("______________________________________________")
}
